<template>
  <el-container>
    <el-header>
      <el-menu :default-active="active" mode="horizontal" background-color="#409EFF" text-color="#fff" active-text-color="#ffd04b" style="width:100%;">
        <el-menu-item index="home" @click="go('/')">首页</el-menu-item>
        <el-menu-item index="events" @click="go('/events')">活动列表</el-menu-item>
        <el-menu-item index="create" @click="go('/events/create')">新建活动</el-menu-item>
        <el-menu-item index="registrations" @click="go('/registrations')">我的报名</el-menu-item>
        <el-menu-item index="checkins" @click="go('/checkins')">我的签到</el-menu-item>
        <div style="flex:1"></div>
        <template v-if="user">
          <el-sub-menu index="user" style="margin-left:auto;">
            <template #title>
              <span style="color:#fff;">{{ user.name || user.username }}</span>
            </template>
            <el-menu-item @click="onLogout">退出登录</el-menu-item>
          </el-sub-menu>
        </template>
        <template v-else>
          <el-menu-item index="login" style="margin-left:auto;" @click="dialogVisible = true">登录/注册</el-menu-item>
        </template>
      </el-menu>
    </el-header>
    <el-main>
      <router-view />
    </el-main>
    <el-dialog v-model="dialogVisible" width="400px" :show-close="true" center>
      <template #title>
        <el-tabs v-model="tab" stretch>
          <el-tab-pane label="登录" name="login" />
          <el-tab-pane label="注册" name="register" />
        </el-tabs>
      </template>
      <div v-if="tab === 'login'">
        <el-form :model="loginForm" @submit.prevent="onLogin" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="loginForm.username" required />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="loginForm.password" type="password" required />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit" @click="onLogin">登录</el-button>
          </el-form-item>
        </el-form>
        <el-alert v-if="loginMsg" type="error" :closable="false" show-icon>{{ loginMsg }}</el-alert>
      </div>
      <div v-else>
        <el-form :model="registerForm" @submit.prevent="onRegister" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="registerForm.username" required />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="registerForm.password" type="password" required />
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="registerForm.name" required />
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="registerForm.email" required />
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="registerForm.phone" required />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit" @click="onRegister">注册</el-button>
          </el-form-item>
        </el-form>
        <el-alert v-if="registerMsg" type="success" :closable="false" show-icon>{{ registerMsg }}</el-alert>
      </div>
    </el-dialog>
  </el-container>
</template>

<script setup lang="ts">
import { ref, watch, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { userApi } from './api/user'
import { ArrowDown } from '@element-plus/icons-vue'
const route = useRoute()
const router = useRouter()
const active = ref('home')
watch(route, () => {
  active.value = route.path.split('/')[1] || 'home'
})
const go = (path: string) => router.push(path)

const dialogVisible = ref(false)
const tab = ref('login')
const loginForm = ref({ username: '', password: '' })
const registerForm = ref({ username: '', password: '', name: '', email: '', phone: '' })
const loginMsg = ref('')
const registerMsg = ref('')
const user = ref<any>(null)

const loadUser = () => {
  const u = localStorage.getItem('user')
  user.value = u ? JSON.parse(u) : null
}
onMounted(loadUser)

const onLogin = async () => {
  try {
    const res = await userApi.login(loginForm.value)
    if (res && res.userId) {
      localStorage.setItem('user', JSON.stringify(res))
      loginMsg.value = '登录成功！'
      dialogVisible.value = false
      loadUser()
      window.location.href = '/'
    } else {
      loginMsg.value = '登录失败，用户信息异常'
    }
  } catch (e) {
    loginMsg.value = '登录失败，请检查用户名或密码'
  }
}

const onRegister = async () => {
  await userApi.register(registerForm.value)
  registerMsg.value = '注册成功！'
}

const onLogout = () => {
  localStorage.removeItem('user')
  user.value = null
  window.location.href = '/'
}
</script> 